﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BB.Apostas.Models;
using BB.Apostas.ViewModels;
using WebMatrix.WebData;
using System.Web.Security;
using System.Web.UI;
using BB.Apostas.Filters;
using EntityFramework.Extensions;

namespace BB.Apostas.Controllers
{
    [Authorize]
    [InitializeSimpleMembership]
    public class RegistoController : Controller
    {
        private ApostasBalasDbContext db = new ApostasBalasDbContext();

        public ActionResult Index()
        {
            var viewmodel = new RegistoViewModel
            {
                competicoes = db.Competicoes.ToList(),
                registos = db.Registos.Where(r=>r.UserId == WebSecurity.CurrentUserId).ToList()
            };
            return View(viewmodel);
        }

        [ActionName("Registar")]
        public ActionResult Register(int id = 0)
        {
            if (ModelState.IsValid)
            {
                var iExists = db.Registos
                    .Where(r => r.CompeticaoId == id && r.UserId == WebSecurity.CurrentUserId).Any();
                if (iExists)
                {
                    return RedirectToAction("Index");
                }
                Registo registo = new Registo();
                registo.Competicao = db.Competicoes.Find(id);
                registo.UserId = WebSecurity.CurrentUserId;
                registo.Active = false;

                db.Registos.Add(registo);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return RedirectToAction("Index");
        }

        [ActionName("Eliminar")]
        public ActionResult Delete(int id = 0)
        {
            Registo registo = db.Registos.Find(id);
            db.Registos.Remove(registo);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        [ActionName("Activar")]
        public ActionResult Activate(int id = 0)
        {
            db.Registos             
             .Where(i => i.UserId == WebSecurity.CurrentUserId)
             .Update(i => new Registo { Active = false });
            ChangeState(id, true);
            return RedirectToAction("Index");
        }

        [ActionName("Desactivar")]
        public ActionResult Deactivate(int id = 0)
        {
            //if (db.Registos                    
            //        .Where(i => i.UserId == WebSecurity.CurrentUserId && i.Active == true).Any())
            //    return RedirectToAction("Index");

            ChangeState(id, false);
            return RedirectToAction("Index");
        }

        private void ChangeState(int id, bool state)
        {
            Registo registo = db.Registos.Find(id);
            registo.Active = state;
            db.Entry(registo).State = EntityState.Modified;
            db.SaveChanges();
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}